package net.wofly.finance.expense.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.finance.expense.domain.ExpenseAccount;
import net.wofly.finance.expense.service.IExpenseAccountService;
import net.wofly.finance.expense.validator.ExpenseAccountValidator;
import net.wofly.framework.dao.PageWrapper;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.stock.domain.ResourceAsk;
import net.wofly.sale.stock.validator.ResourceAskValidator;

@Controller
@RequestMapping(value = "/api/finance/expense")
@Api(description = "报销管理")
public class ExpenseAccountController {
	
	Logger logger = LoggerFactory.getLogger(ExpenseAccountController.class);
    
	@Autowired
	private IExpenseAccountService iExpenseAccountService ;
    
    /**
	 * 
	 * @param expenseAccount
	 */
    @ApiOperation(value = "新增报销单")
    @RequestMapping(value = "/saveExpenseAccount", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "expenseAccount", value = "报销单实体", required = true, dataType ="ExpenseAccount", paramType = "body")})
    @ResponseBody
	public ReturnBean<ExpenseAccount> saveExpenseAccount(@RequestBody ExpenseAccount expenseAccount){
    	
    		String errorMessage = ExpenseAccountValidator.validateSaveExpenseAccountParams(expenseAccount) ;
        
    		if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        return iExpenseAccountService.saveExpenseAccount(expenseAccount) ;
    }
    
    /**
	 * 
	 * @param expenseAccount
	 */
    @ApiOperation(value = "更新报销单")
    @RequestMapping(value = "/updateExpenseAccount", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "expenseAccount", value = "报销单实体", required = true, dataType ="ExpenseAccount", paramType = "body")})
    @ResponseBody
	public ReturnBean<ExpenseAccount> updateExpenseAccount(@RequestBody ExpenseAccount expenseAccount){
    	
    		String errorMessage = ExpenseAccountValidator.validateUpdateExpenseAccountParams(expenseAccount) ;
        
    		if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        return iExpenseAccountService.updateExpenseAccount(expenseAccount) ;
    }

	/**
	 * 按时间倒序
	 * 
	 * @param startTime
	 * @param endTime
	 */
    @ApiOperation(value = "获取我的报销单")
    @RequestMapping(value = "/getMyExpenseAccount", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "askUserID", value = "当前用户ID", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType ="Long", paramType = "form"),
    						@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType ="Long", paramType = "form") ,
					    @ApiImplicitParam(name = "pageNum", value = "分页页码", required = true, dataType ="int", paramType = "form"),
						@ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, dataType ="int", paramType = "form")
    							})
    @ResponseBody
	public ReturnBean<PageWrapper<ExpenseAccount>> getMyExpenseAccount(String askUserID ,Long startTime, Long endTime, Integer pageNum, Integer pageSize){
    		
    		String errorMessage = ExpenseAccountValidator.validateAskUserIDParams(askUserID) ;
	    if (errorMessage.length() > 0) {
	        return new ReturnBean<>(-1, errorMessage);
	    }
	    
    		return iExpenseAccountService.getMyExpenseAccount(askUserID ,startTime, endTime ,pageNum, pageSize) ;
    }

	/**
	 * 按时间倒序
	 * 
	 * @param orgID
	 * @param askUserID
	 * @param startTime
	 * @param endTime
	 */
    @ApiOperation(value = "获取全部报销单")
    @RequestMapping(value = "/getAllExpenseAccount", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "orgID", value = "部门ID", required = false, dataType ="String", paramType = "form"),
    		@ApiImplicitParam(name = "askUserID", value = "申请人ID", required = false, dataType ="String", paramType = "form"),
    		@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType ="Long", paramType = "form"),
    		@ApiImplicitParam(name = "endTime", value = "开始时间", required = false, dataType ="Long", paramType = "form"),
    		@ApiImplicitParam(name = "pageNum", value = "分页页码", required = true, dataType ="int", paramType = "form"),
		@ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, dataType ="int", paramType = "form")
			})
    @ResponseBody
	public ReturnBean<PageWrapper<ExpenseAccount>> getAllExpenseAccount(String orgID, String askUserID, Long startTime, Long endTime, Integer pageNum, Integer pageSize){
    		
    		return iExpenseAccountService.getAllExpenseAccount(orgID, askUserID, startTime, endTime, pageNum, pageSize) ;
    }
    
    @ApiOperation(value = "获取报销单详情")
    @RequestMapping(value = "/getExpenseAccount", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "expenseAccountID", value = "报销申请单ID", required = true, dataType ="String", paramType = "form")})
    @ResponseBody
	public ReturnBean<ExpenseAccount> getExpenseAccount(String expenseAccountID){
	    	String errorMessage = ExpenseAccountValidator.validateExpenseAccountIDParams(expenseAccountID) ;
	    if (errorMessage.length() > 0) {
	        return new ReturnBean<>(-1, errorMessage);
	    }
	    
    		return iExpenseAccountService.getExpenseAccount(expenseAccountID) ;
    }
    
    @ApiOperation(value = "发起报销单申请工作流", notes = "发起报销单申请工作流")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "申请人ID", required = true, dataType = "String", paramType = "form") ,
    						@ApiImplicitParam(name = "expenseAccountID", value = "报销单ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/startWorkflow", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> startWorkflow(HttpServletRequest request ,String userId ,String expenseAccountID) {
    		Map<String, Object> variables = new HashMap<>() ;
    		
    		String errorMessage = ResourceAskValidator.validateStartWF(userId,expenseAccountID);
    		if (StringUtils.isNotEmpty(errorMessage)) {
                return new ReturnBean<>(1, errorMessage);
    		}
    		
		return iExpenseAccountService.startWorkflow(userId ,expenseAccountID, variables) ;
    }
	
    @ApiOperation(value = "获取报销单申请待办", notes = "获取报销单申请待办")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "申请人ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/findToDoList", method = {RequestMethod.POST})
    @ResponseBody
	public ReturnBean<List<ExpenseAccount>> findToDoList(String userId) {
		
		return iExpenseAccountService.findToDoList(userId) ;
	}
	
    @ApiOperation(value = "完成报销单申请工作流任务", notes = "完成报销单申请工作流任务")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "申请人ID", required = true, dataType = "String", paramType = "form"),
    						@ApiImplicitParam(name = "taskID", value = "任务ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/complete", method = {RequestMethod.POST})
    @ResponseBody
	public ReturnBean<String> complete(HttpServletRequest request ,String userID ,String taskID) {
    	
		Map<String, String[]> mapRequest = request.getParameterMap() ;
		
		return iExpenseAccountService.complete(userID, taskID, mapRequest) ;
	}
	
    @ApiOperation(value = "为工作流获取报销单申请详情", notes = "为工作流获取报销单申请详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "expenseAccountID", value = "报销单申请ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/getExpenseAccountIDForFlow", method = {RequestMethod.POST})
    @ResponseBody
	public ReturnBean<ExpenseAccount> getExpenseAccountIDForFlow(String expenseAccountID) {
		return iExpenseAccountService.getExpenseAccountForFlow(expenseAccountID) ;
	}
}
